<template>
  <!-- 登录页面 -->
  <div id="login">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 输入表单 -->
      <el-form
        :model="form"
        :rules="formRules"
        ref="loginInfo"
        label-width="0px"
      >
        <el-form-item prop="username">
          <el-input v-model="form.username">
            <i slot="prefix" class="iconfont icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password">
            <i slot="prefix" class="iconfont icon-3702mima"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="getLogin">登录</el-button>
          <el-button type="info" @click="resForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456',
      },
      formRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '密码的长度是 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    // 重置表单
    resForm() {
      this.$refs.loginInfo.resetFields()
    },
    //获取表单数据
    getLogin() {
      this.$refs.loginInfo.validate(async (valid) => {
        // 如果表单都符合验证规则，valid才会是true
        if (!valid) return this.$message.error('表单验证失败')
        const { data: res } = await this.$http.post('login', this.form)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        // 将登录成功后的 token，保存到客户端的 sessionStorage中
        // 项目中除了登录之外的其他API接口，必须在登录之后才能访问
        // token只应当在当前完网站打开期间生效,所有将token保存到 sessionStorage中
        window.sessionStorage.setItem('token', res.data.token) //保存客户端
        // 登录成功后,跳转路由
        this.$router.push('/home')
      })
    },
  },
}
</script>
<style lang="less" scoped>
#login {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;
}
.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 304px;
  background-color: #fff;
  box-shadow: 0px 0px 10px #ddd;
}
.login-box .avatar-box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 130px;
  height: 130px;
  padding: 10px;
  border-radius: 50%;
  background-color: #fff;
  box-shadow: 0 0 15px #ddd;
}
.avatar-box img {
  width: 130px;
  height: 130px;
  border-radius: 50%;
  background-color: #efefef;
}
.login-box .el-form {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.el-form .btns {
  float: right;
}
</style>